package com.summerbird.mallchat.common.chat.service.strategy.frequency;

import com.summerbird.mallchat.common.common.exception.BusinessException;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class FrequencyControlStrategyFactory {

    // 目前就3个策略类：固定窗口、滑动窗口、令牌桶
    public static Map<String, AbstractFrequencyControlStrategy> frequencyControlStrategyMap = new ConcurrentHashMap<>(5);

    public static <K extends FrequencyControlDTO> AbstractFrequencyControlStrategy getFrequencyControlStrategyByName(String strategyName) {
        AbstractFrequencyControlStrategy strategy = frequencyControlStrategyMap.get(strategyName);
        if(Objects.isNull(strategy)){
            throw new BusinessException("限流策略不存在");
        }
        return strategy;
    }

    public static <K extends FrequencyControlDTO> void registerFrequencyControlStrategy(String strategyName, AbstractFrequencyControlStrategy strategy) {
        frequencyControlStrategyMap.put(strategyName, strategy);
    }
}
